﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

internal enum Functions
{
    Position,
    Scale,
    RotAxis,
    RotSpeed,
    Color,
    Opacity,
    SmoothColor
}

public class CubeMod : MonoBehaviour
{
    public Transform cube;
    public Button[] btns;

    public Button btnReset;

    private MeshRenderer renderer;
    private Material material;
    private Vector3 axis;
    private float speed;
    private bool smoothColor = true;
    private Color targetColor;

    public float moveSpeed = 100.0f;
    public float colorSpeed = 1f;
    
    private void Awake()
    {
        renderer = cube.gameObject.GetComponent<MeshRenderer>();
        material = renderer.material;

        for (int i = 0; i < btns.Length; i++)
        {
            var func = (Functions) i;
            var btn = btns[i];
            btn.GetComponentInChildren<Text>().text = func.ToString();
            btn.onClick.AddListener(()=>OnClickButton(func));
        }
        
        btnReset.onClick.AddListener(OnReset);
    }

    private void OnReset()
    {
        cube.position = Vector3.zero;
        cube.localScale = Vector3.one;
        material.color = Color.white;
        axis = Vector3.up;
        speed = 1.0f;
    }

    private void OnClickButton(Functions func)
    {
        float randX = Random.Range(-1.0f, 1.0f);
        float randY = Random.Range(-1.0f, 1.0f);
        float randZ = Random.Range(-1.0f, 1.0f);
        Vector3 value = new Vector3(randX, randY, randZ);
        Debug.Log($"OnClicked: {func} : {value}");
        switch (func)
        {
            case Functions.Position:
                cube.position = new Vector3(randX, randY, randZ);
                break;
            case Functions.Scale:
                cube.localScale = Vector3.one * randX;
                break;
            case Functions.RotAxis:
                axis = value;
                break;
            case Functions.RotSpeed:
                speed = randX;
                break;
            case Functions.Color:
                var oldAlpha = material.color.a; 
                material.color = new Color(value.x, value.y, value.z, oldAlpha);
                smoothColor = false;
                break;
            case Functions.Opacity:
                material.color = new Color(material.color.r, material.color.g, material.color.b, randX);
                break;
            case Functions.SmoothColor:
                smoothColor = true;
                targetColor = new Color(randX, randY, randZ, material.color.a);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(func), func, null);
        }
    }

    void Start()
    {
        OnReset();
        
        // transform.position = new Vector3(3, 4, 1);
        // transform.localScale = Vector3.one * 1.3f;
        //
        // Material material = Renderer.material;
        //
        // material.color = new Color(0.5f, 1.0f, 0.3f, 0.4f);
    }

    bool IsSameColor(Color srcColor, Color dstColor)
    {
        return Mathf.Abs(srcColor.r - dstColor.r) < 0.1f && Mathf.Abs(srcColor.g - dstColor.g) < 0.1f && Mathf.Abs(srcColor.b - dstColor.b) < 0.1f;
    }
    
    void Update()
    {
        cube.Rotate(axis, Time.deltaTime * speed * moveSpeed);
        if (smoothColor)
        {
            material.color = Color.Lerp(material.color, targetColor, Time.deltaTime * colorSpeed);
            if (IsSameColor(material.color, targetColor))
            {
                OnClickButton(Functions.SmoothColor);
            }
        }
    }
}
